﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Shapes;
using PairingMatrix.Common.Enum;
using PairingMatrix.ViewModel;

namespace PairingMatrix.View.Behavior
{
    public class SetPositionOfMingleCardBehavior : Behavior<MingleCardControl>
    {
        private ContentPresenter _parent;
        private Canvas _canvas;
        private MingleCardViewModel _mingleCardViewModel;


        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (sender, e) =>
                                       {
                                           _mingleCardViewModel = (MingleCardViewModel) AssociatedObject.DataContext;
                                           _mingleCardViewModel.PropertyChanged += MingleCardPropertyChanged;

                                            SetCardInPosition();
                                       };


        }

        void MingleCardPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "Position")
            {
                SetCardInPosition();
            }
        }

        private void SetCardInPosition()
        {
            if (_parent == null)
            {
                _parent = (ContentPresenter)VisualTreeHelper.GetParent(AssociatedObject);
                _canvas = (Canvas)VisualTreeHelper.GetParent(_parent);
            }
            Canvas.SetTop(_parent, _mingleCardViewModel.Position.Y);
            Canvas.SetLeft(_parent, _mingleCardViewModel.Position.X);
        }
    }
}
